package com.hzw.saas.common.config.util;


import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiConsumer;

public final class MapUtil {
    public static boolean isNotEmpty(Map<?, ?> map) {
        return map != null && !map.isEmpty();
    }

    public static boolean isEmpty(Map<?, ?> map) {
        return !isNotEmpty(map);
    }

    public static <K, V> void forEach(Map<K, V> map, BiConsumer<Object, Object> action) {
        if (!isEmpty(map) && action != null) {
            Object k;
            Object v;
            for(Iterator var2 = map.entrySet().iterator(); var2.hasNext(); action.accept(k, v)) {
                Entry entry = (Entry)var2.next();

                try {
                    k = entry.getKey();
                    v = entry.getValue();
                } catch (IllegalStateException var7) {
                    throw new ConcurrentModificationException(var7);
                }
            }

        }
    }

    public static String parseMapToString(Map<String, String> params, boolean encode) {
        List<String> paramList = new ArrayList();
        forEach(params, (k, v) -> {
            if (v == null) {
                paramList.add(k + "=");
            } else {
                paramList.add(k + "=" + (encode ? UrlUtil.urlEncode((String) v) : v));
            }

        });
        return String.join("&", paramList);
    }

    public static Map<String, String> parseStringToMap(String str, boolean decode) {
        str = preProcess(str);
        Map<String, String> params = new HashMap(16);
        if (StringUtil.isEmpty(str)) {
            return params;
        } else if (!str.contains("&")) {
            params.put(decode(str, decode), "");
            return params;
        } else {
            int len = str.length();
            String name = null;
            int pos = 0;

            int i;
            for(i = 0; i < len; ++i) {
                char c = str.charAt(i);
                if (c == '=') {
                    if (null == name) {
                        name = str.substring(pos, i);
                    }

                    pos = i + 1;
                } else if (c == '&') {
                    if (null == name && pos != i) {
                        addParam(params, str.substring(pos, i), "", decode);
                    } else if (name != null) {
                        addParam(params, name, str.substring(pos, i), decode);
                        name = null;
                    }

                    pos = i + 1;
                }
            }

            if (pos != i) {
                if (name == null) {
                    addParam(params, str.substring(pos, i), "", decode);
                } else {
                    addParam(params, name, str.substring(pos, i), decode);
                }
            } else if (name != null) {
                addParam(params, name, "", decode);
            }

            return params;
        }
    }

    private static void addParam(Map<String, String> params, String key, String value, boolean decode) {
        key = decode(key, decode);
        value = decode(value, decode);
        if (params.containsKey(key)) {
            params.put(key, (String)params.get(key) + "," + value);
        } else {
            params.put(key, value);
        }

    }

    private static String decode(String str, boolean decode) {
        return decode ? UrlUtil.urlDecode(str) : str;
    }

    private static String preProcess(String str) {
        if (StringUtil.isEmpty(str)) {
            return str;
        } else {
            int beginPos = str.indexOf("?");
            if (beginPos > -1) {
                str = str.substring(beginPos + 1);
            }

            int endPos = str.indexOf("#");
            if (endPos > -1) {
                str = str.substring(0, endPos);
            }

            return str;
        }
    }

    private MapUtil() {
        throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
    }
}
